package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    @GetMapping("/allData")
    public ResultData allData(){
        List<SysMenuDto> sysMenuDtos = iSysMenuService.allData();
        return ResultData.ok("查询成功",sysMenuDtos);
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody SysMenu sysMenu){
        sysMenu.setIsShow(1);
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        iSysMenuService.save(sysMenu);
        return ResultData.ok("添加成功",null);
    }

    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        SysMenu byId = iSysMenuService.getById(id);
        return ResultData.ok("查询成功",byId);
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdateDate(LocalDateTime.now());
        iSysMenuService.updateById(sysMenu);
        return ResultData.ok("修改成功",null);
    }

    @GetMapping("/del")
    public ResultData delete(String id){
        LambdaQueryWrapper<SysMenu> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(SysMenu::getId,id);
        queryWrapper.eq(SysMenu::getDefaultData,1);
        SysMenu one = iSysMenuService.getOne(queryWrapper);
        if (one!=null){
            return ResultData.error("默认数据不允许删除");
        }
        iSysMenuService.removeById(id);
        return ResultData.ok("删除成功！",null);
    }
}
